package com.shadowtorcher.TableRolePlayingSystem.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.concurrent.ThreadLocalRandom;

public class Security {
    public static final String publicKeyString = "-----BEGIN PUBLIC KEY-----\n" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJ4uojnuFlX3dXn7L4UFs7zvJ2\n" +
            "m7BrTzteWHjKCbzILHb5DO9+WjOsenFKKfgxIzjUiE2tq4zaUxLKLCTc2i5XaGY+\n" +
            "gJ7sYe5dwoa8n5jYDkR+K2f8LA8H/HE7pelG+ddlXSBXS4rnjVAkpfIu4CiXOURo\n" +
            "5iQD6a/DUc55xDUAQQIDAQAB\n" +
            "-----END PUBLIC KEY-----";

    public static final String privateKeyString = "-----BEGIN RSA PRIVATE KEY-----\n" +
            "MIICXgIBAAKBgQDJ4uojnuFlX3dXn7L4UFs7zvJ2m7BrTzteWHjKCbzILHb5DO9+\n" +
            "WjOsenFKKfgxIzjUiE2tq4zaUxLKLCTc2i5XaGY+gJ7sYe5dwoa8n5jYDkR+K2f8\n" +
            "LA8H/HE7pelG+ddlXSBXS4rnjVAkpfIu4CiXOURo5iQD6a/DUc55xDUAQQIDAQAB\n" +
            "AoGBAKtJr0YRo0MTQHpDsOElrIKODwG7Dm+9ha2CetlQca+UqkycQQZ/GaCZMk2u\n" +
            "Vjm6nJkWHHHhP1xtZ1/JHG2VYAKehriiM7yltSQRSd1Ghh8u0d7fyQV6N/JXjJGR\n" +
            "P38gWA1FHxxcRr13jwqbM/M6ghjKd66V++92+93lML25t27RAkEA88p3JT+3mzm2\n" +
            "UkLFwNH3FnAq7Gy4gqdNEg+KDhJvdke2GHHvJyz3pB2Py1dVnUdGw7+ZNw/AQWNh\n" +
            "7DB3n4TaHwJBANP/NdR0e1ub1vDDVenBa3hUu1NGvtS/8J5nB3oJjsDVu0Bs6bN2\n" +
            "g7Ayqd/lmzAujdGBFh4BU3aYyL4J1rULmZ8CQQCZo6FO0kcOBmMGmJo0GJ7Vfz8t\n" +
            "3J6ziGgj17Mzhx7DthHmefc0AzzWKuMyTQnhosfQGXGfp8eL7ewKg8j/uSQVAkAh\n" +
            "UW7oYRYyI5/4XvrVrixx0P9P60J6tV/fxVmLeI0TCYRQb82bAvlXa97+7Vsii26G\n" +
            "ir8rIBI6BXEnTOyNr7EJAkEAkspEEjut9JqN21Tyo66aLZ0HXJWq36fSNbCNAMcH\n" +
            "VrGn5+lQqyHzzJvY7g3WCEur52bBCL7QUPbNFw5itdmu8g==\n" +
            "-----END RSA PRIVATE KEY-----";

    public static final String privateKeyString_pkcs8 = "-----BEGIN PRIVATE KEY-----\n" +
            "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMni6iOe4WVfd1ef\n" +
            "svhQWzvO8nabsGtPO15YeMoJvMgsdvkM735aM6x6cUop+DEjONSITa2rjNpTEsos\n" +
            "JNzaLldoZj6Anuxh7l3ChryfmNgORH4rZ/wsDwf8cTul6Ub512VdIFdLiueNUCSl\n" +
            "8i7gKJc5RGjmJAPpr8NRznnENQBBAgMBAAECgYEAq0mvRhGjQxNAekOw4SWsgo4P\n" +
            "AbsOb72FrYJ62VBxr5SqTJxBBn8ZoJkyTa5WObqcmRYcceE/XG1nX8kcbZVgAp6G\n" +
            "uKIzvKW1JBFJ3UaGHy7R3t/JBXo38leMkZE/fyBYDUUfHFxGvXePCpsz8zqCGMp3\n" +
            "rpX773b73eUwvbm3btECQQDzynclP7ebObZSQsXA0fcWcCrsbLiCp00SD4oOEm92\n" +
            "R7YYce8nLPekHY/LV1WdR0bDv5k3D8BBY2HsMHefhNofAkEA0/811HR7W5vW8MNV\n" +
            "6cFreFS7U0a+1L/wnmcHegmOwNW7QGzps3aDsDKp3+WbMC6N0YEWHgFTdpjIvgnW\n" +
            "tQuZnwJBAJmjoU7SRw4GYwaYmjQYntV/Py3cnrOIaCPXszOHHsO2EeZ59zQDPNYq\n" +
            "4zJNCeGix9AZcZ+nx4vt7AqDyP+5JBUCQCFRbuhhFjIjn/he+tWuLHHQ/0/rQnq1\n" +
            "X9/FWYt4jRMJhFBvzZsC+Vdr3v7tWyKLboaKvysgEjoFcSdM7I2vsQkCQQCSykQS\n" +
            "O630mo3bVPKjrpotnQdclarfp9I1sI0AxwdWsafn6VCrIfPMm9juDdYIS6vnZsEI\n" +
            "vtBQ9s0XDmK12a7y\n" +
            "-----END PRIVATE KEY-----\n";

    public static PublicKey getPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
        String publicKeyContent = publicKeyString.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----\n", "").replace("\n-----END PUBLIC KEY-----\n", "");
        X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent));
        return KeyFactory.getInstance("RSA").generatePublic(keySpecX509);
    }

    public static PrivateKey getPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
        String privateKeyContent = privateKeyString_pkcs8.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
        PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyContent));
        return KeyFactory.getInstance("RSA").generatePrivate(keySpecPKCS8);
    }

    public static String getRandom() {
        StringBuilder builder = new StringBuilder(20);
        for (int i = 0; i < 20; i++) {
            builder.append((char) (ThreadLocalRandom.current().nextInt(33, 128)));
        }
        return builder.toString();
    }

    private static Cipher serverCipher = null;

    public static Cipher getServerDecodeCipher() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
        if(serverCipher == null){
            java.security.Security.addProvider(new BouncyCastleProvider());
            serverCipher = Cipher.getInstance("RSA/None/PKCS1Padding");
            serverCipher.init(Cipher.DECRYPT_MODE, Security.getPrivateKey());
        }
        return serverCipher;
    }
}
